package com.wugui.datax.rpc.remoting.net;

import com.wugui.datax.rpc.remoting.net.params.BaseCallback;
import com.wugui.datax.rpc.remoting.provider.XxlRpcProviderFactory;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * server
 *
 * @author xuxueli 2015-11-24 20:59:49
 */
@Setter
@Slf4j
public abstract class Server {

  private BaseCallback startedCallback;

  private BaseCallback stoppedCallback;

  /**
   * start server
   *
   * @param xxlRpcProviderFactory
   * @throws Exception
   */
  public abstract void start(final XxlRpcProviderFactory xxlRpcProviderFactory) throws Exception;

  /** callback when started */
  public void onStarted() {
    if (startedCallback != null) {
      try {
        startedCallback.run();
      } catch (Exception e) {
        log.error(">>>>>>>>>>> xxl-rpc, server startedCallback error.", e);
      }
    }
  }

  /**
   * stop server
   *
   * @throws Exception
   */
  public abstract void stop() throws Exception;

  /** callback when stoped */
  public void onStopped() {
    if (stoppedCallback != null) {
      try {
        stoppedCallback.run();
      } catch (Exception e) {
        log.error(">>>>>>>>>>> xxl-rpc, server stoppedCallback error.", e);
      }
    }
  }
}
